package vip.xiaonuo.smzq.modular.hisi.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import vip.xiaonuo.smzq.modular.hisi.entity.Kshz;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import vip.xiaonuo.smzq.modular.hisi.entity.vo.ChartVo;
import vip.xiaonuo.smzq.modular.hisi.entity.vo.KsQueryCriteria;
import vip.xiaonuo.smzq.modular.hisi.entity.vo.KsxxhzVo;
import vip.xiaonuo.smzq.modular.kcl.entity.Ks;
import vip.xiaonuo.smzq.modular.kcl.entity.param.KszhlsVo;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.KsAutocompleteVo;

import java.util.List;
import java.util.Map;

/**
 * 矿山信息汇总 Mapper
 *
 * @author sqq
 * @since 2024-07-04
 */
@Mapper
public interface KshzMapper extends MPJBaseMapper<Kshz> {

    @Select("\twith hz as (\t\t\t\t\t\n" +
            "SELECT kqbh,kcdm,nd,tjdx FROM hisi_kshz   " +
            "${ew.customSqlSegment} " +
            "GROUP BY kqbh,kcdm,nd,tjdx ORDER BY nd)\n" +
            "\n" +
            "SELECT hz.nd,CAST(sum(case when c.ksldw is not null then zy.nmbyksl else zy.nmbyjsl end) AS DECIMAL(18,2)) as value \n" +
            "              from hz \n" +
            "             left join  hisi_ks_zyclhz zy on hz.nd = zy.nd and hz.kqbh = zy.kqbh and hz.kcdm = zy.kcdm and hz.tjdx=zy.tjdx \n" +
            "\t\t\t\t\t\t left join kcl_cb06_kc c on hz.kcdm = c.kcdm and hz.tjdx = c.tjdx\n" +
            "\t\t\t\t\t\t where zy.nd is not null\n" +
            "\t\t\t\t\t\t and zy.zycllx in ('TM', 'TD', 'KZ')\n" +
            " group by hz.nd\n" +
            " order by hz.nd")
    List<ChartVo> lineChartXian(@Param(Constants.WRAPPER) Wrapper<Map> queryWrapper);


    @Select("SELECT\n" +
            "\tkcmc AS NAME,\n" +
            "\tCOUNT ( subquery ) AS \n" +
            "VALUE\n" +
            "\t\n" +
            "FROM\n" +
            "\t(\n" +
            "\tSELECT\n" +
            "\t\thz.kqbh,\n" +
            "\t\thz.kcmc\n" +
            "\tFROM\n" +
            "\t\thisi_kshz hz\n" +
            "\t${ew.customSqlSegment}\n" +
            "\tGROUP BY\n" +
            "\t\thz.kqbh,\n" +
            "\t\thz.kcmc\n" +
            "\t) AS subquery \n" +
            "GROUP BY\n" +
            "\tsubquery.kcmc \n" +
            "ORDER BY\n" +
            "\tsubquery.kcmc")
    List<Map> barChartXianKcd(@Param(Constants.WRAPPER) Wrapper<Map> queryWrapper);

    @Select("select c.mc as name,\n" +
            "count(hz.pkid) as value\n" +
            "from kcl_cb01_zh c\n" +
            "left join hisi_kshz hz on c.mc = hz.kcjdmc\n" +
            "${ew.customSqlSegment}\n" +
            "group by c.mc\n" +
            "order by c.mc")
    List<Map> barChartXianKccd(@Param(Constants.WRAPPER) Wrapper<Map> queryWrapper);

    @Select("select hz.gm as name,\n" +
            "count(hz.pkid) as value\n" +
            "from hisi_kshz hz\n" +
            "${ew.customSqlSegment}\n" +
            "group by hz.gm")
    List<Map> barChartXianGm(@Param(Constants.WRAPPER) Wrapper<Map> queryWrapper);




    @Select("SELECT\n" +
            "\tks.nd,\n" +
            "\tMAX ( ks.xzqdm ) xzqdm,\n" +
            "\tMAX ( ks.kqmc ) kqmc,\n" +
            "\tks.kqbh,\n" +
            "\tks.kcmc,\n" +
            "\tks.jsdxmc,\n" +
            " MAX ( ks.gm) gm,\n" +
            " MAX\t(ks.sfly) sfly,\n" +
            " MAX\t(ks.kczh) kczh,\n" +
            " MAX\t(ks.kcjdmc) kcjdmc, \n" +
            "\tMAX ( zy.jsldw ) jsldw,\n" +
            "\tCAST(sum(ncbyjsl) AS DECIMAL(18,2)) ncbyjsl,\n" +
            "\tCAST(sum(ncljjsl) AS DECIMAL(18,2)) ncljjsl,\n" +
            "\tCAST(sum(kcljsl) AS DECIMAL(18,2)) kcljsl,\n" +
            "\tCAST(sum(ssljsl) AS DECIMAL(18,2)) ssljsl,\n" +
            "\tCAST(sum(kczjjsl) AS DECIMAL(18,2)) kczjjsl,\n" +
            "\tCAST(sum(cszjjsl) AS DECIMAL(18,2)) cszjjsl,\n" +
            "\tCAST(sum(spyfjsl) AS DECIMAL(18,2)) spyfjsl,\n" +
            "\tCAST(sum(otherdatajsl) AS DECIMAL(18,2)) otherdatajsl,\n" +
            "\tCAST(sum(nmbyjsl) AS DECIMAL(18,2)) nmbyjsl,\n" +
            "\tCAST(sum(ljcmjsl) AS DECIMAL(18,2)) ljcmjsl,\n" +
            "\tMAX ( zy.ksldw ) ksldw,\n" +
            "\tCAST(sum(ncbyksl     ) AS DECIMAL(18,2)) ncbyksl,\n" +
            "\tCAST(sum(ncljksl     ) AS DECIMAL(18,2)) ncljksl,\n" +
            "\tCAST(sum(kclksl      ) AS DECIMAL(18,2)) kclksl,\n" +
            "\tCAST(sum(sslksl      ) AS DECIMAL(18,2)) sslksl,\n" +
            "\tCAST(sum(kczjksl     ) AS DECIMAL(18,2)) kczjksl,\n" +
            "\tCAST(sum(cszjksl     ) AS DECIMAL(18,2)) cszjksl,\n" +
            "\tCAST(sum(spyfksl     ) AS DECIMAL(18,2)) spyfksl,\n" +
            "\tCAST(sum(otherdataksl) AS DECIMAL(18,2)) otherdataksl,\n" +
            "\tCAST(sum(nmbyksl     ) AS DECIMAL(18,2)) nmbyksl,\n" +
            "\tCAST(sum(ljcmksl     ) AS DECIMAL(18,2)) ljcmksl\n" +
            "FROM\n" +
            "\thisi_kshz ks\n" +
            "\tLEFT JOIN hisi_ks_zyclhz zy ON ks.nd = zy.nd \n" +
            "\tAND ks.kqbh = zy.kqbh \n" +
            "\tAND ks.djflbh = zy.djflbh \n" +
            "\tAND ks.kcdm = zy.kcdm \n" +
            "\tAND ks.tjdx = zy.tjdx " +
                   " ${sql}")
    List<KsxxhzVo> getlist(String sql);


    @Select("WITH " +
            "KszhlsVo as (select nd, kqbh,ksmc,xzqdm,djflbh,kcmc,ksgm,kcdm from hisi_kshz group by nd, kqbh,ksmc,xzqdm,djflbh,kcmc,ksgm,kcdm) " +
            "select vo.* from KszhlsVo vo ${ew.customSqlSegment}")
    Page<KszhlsVo> getKsPage(Page<Kshz> page, @Param(Constants.WRAPPER) Wrapper<Kshz> queryWrapper);
}
